<?php
namespace Shopmanage\Controller;
use Think\Controller;
class EventController extends ExtendController {
	public function index(){
		Vendor("Page");
		$toolname = I("toolname");
		$q = I("q");
		$t = I("t");
		$CFG = M("ShopCfg");
		$cfg = current($CFG->where("`sid`=".session("SID"))
						   ->getField("id,curtool,qrcode"));
		$curtool = $cfg["curtool"];
		$qrcode = $cfg["qrcode"];
		if(!$qrcode){
			Vendor('taobaosdk.top.request.MaQrcodeCommonCreateRequest');
			$req = new \MaQrcodeCommonCreateRequest;
			$req->setType("url");
			$req->setContent(C("URL")."?m=shoptaemob&sid=".$this->sid);
			$req->setName("多麦抽奖");
			$req->setChannelName("移动端装修");
			$req->setStyle("EF4F2B");
			$req->setSize("300");
			$req->setNeedEps("false");
			$req->setLogo("http://img01.taobaocdn.com/imgextra/T1Od8YFT8eXXXXXXXX");
			$resp = $this->c->execute($req, session("access_token"));
			if(C("TAE")){
				$qrcode = (String)$resp->modules->qrcode_d_o[0]->qrcode_url;
			}else{
				$qrcode = (String)$resp->modules->qrcode_d_o->qrcode_url;
			}
			if($qrcode){
				$CFG->where(Array("sid"=>$this->sid))->save(Array("qrcode"=>$qrcode));
			}else{
				$qrcode="生成二维码出错！请重新登录".$resp->msg.$resp->sub_msg;
			}
		}
		
		$Tool = D("CheckTools");
		$toolname = $toolname?$toolname:$curtool;
		if($toolname=="kxdr"){
			$this->redirect("Kxdr/Index/Index");
			die();
		}
		$tool_info = current($Tool->GetToolInfo($toolname));
		if($q){
			$where = $q?" AND `start_time`>'".date("Y-m-d H:i:s",strtotime($q))."' AND `start_time`<'".date("Y-m-d H:i:s",strtotime($q)+86400)."'":"";
		}elseif($t){
			switch($t){
				case 1:$where = " AND `start_time`<'".date("Y-m-d H:i:s")."' AND `end_time`>'".date("Y-m-d H:i:s")."'";break;
				case 2:$where = " AND `start_time`>'".date("Y-m-d H:i:s")."'";break;
				case 3:$where = " AND `end_time`<'".date("Y-m-d H:i:s")."'";break;
			}
		}
		
		$EV = M("Events");
		$total = $EV->where("`sid`=".$this->sid.$where)->getField("count(`id`)"); 
		$PAGE = new \Page($total,10);
		$page = $PAGE->show();
		$events = $EV->where("`sid`=".$this->sid.$where)->order("`id` DESC")->limit($PAGE->firstRow.','.$PAGE->listRows)->getField("`id`,`curtool`, `mobtool`, `start_time`, `end_time`, `range_time`, `withdraw`");
		
		$EGIFT = D("EventsGifts");
		foreach($events as $k=>$event){
			if(strtotime($events[$k]["end_time"])<=time()){
				$events[$k]["status"]="stopped";
			}elseif(strtotime($events[$k]["start_time"])>=time()){
				$events[$k]["status"]="ready";
			}elseif(strtotime($events[$k]["start_time"])<time()&&strtotime($events[$k]["end_time"])>time()){
				$events[$k]["status"]="running";
				$curtool = $events[$k]["curtool"];
			}
			if(strtotime($events[$k]["end_time"])>0){
				$events[$k]["end_time_str"]=GetTimeInfo(strtotime($events[$k]["end_time"]));
				$events[$k]["start_time_str"]=GetTimeInfo(strtotime($events[$k]["start_time"]));
			}else{
				$events[$k]["end_time_str"]="时间出错！请删除重设";
			}
			$events[$k]["curtool_info"]=current($Tool->GetToolInfo($events[$k]["curtool"]));;
			
			$EGIFT->sid=$this->sid;
			$EGIFT->eid=$k;
			$EGIFT->getEventGifts();
			$gifts = $EGIFT->gifts;
			$gift_names = $EGIFT->gift_names;
			$gift_ids = $EGIFT->gift_ids;
			
			$events[$k]["gifts"]=$gifts;
			$events[$k]["gift_quant"]=count($gift_names);
			$events[$k]["gift_type"]=count($gifts);
			$events[$k]["gift_ids"]=json_encode(array_merge($gift_ids));
			$events[$k]["gift_names"]=$gift_names;
			
			//判断是否是支持移动外观的
			$has_mob = C("HAS_MOB");
			$events[$k]["has_mob"]=in_array($events[$k]["curtool"],$has_mob);

			unset($gift_ids);
			unset($gift_names);
		}
		if(!$curtool){
			$curtool="eggs";
		}
		
		$P = M("Page");
		$info["msg"]=htmlspecialchars_decode($P->where("`id`=1")->getField("`content`"));
		$info["type"]="success";
		if($info["msg"]){
		$this->assign("info",$info);
		}
		
		$daily = current($CFG->where("`sid`=".$this->sid)->order("`id`")->getField("`id`,`curtool`, `start_time`, `end_time`, `range_time`, `withdraw`"));
		$this->assign("q",$q);
		$this->assign("page",$page);
		$this->assign("daily",$daily);
		$this->assign("events",$events);
		$this->assign("curtool",$curtool);
		$this->assign("qrcode",$qrcode);
		$this->assign("tool_info",$tool_info);
		$this->assign("moburl",C("URL")."?m=shoptaemob&sid=".$this->sid);
		$this->display("Event:index");
	}
	//创建之后再选择外观，该步骤已移除
	public function step1(){
		$create = I("create");
		$eid = I("eid");
		$start_time = I("start_time");
		if($create){
			session("lott_event_id",null);
			session("curtool",null);
			session("front_cfg",null);
			unset($_SESSION['lott_event_id']);
		}
		session("from_event_id",$eid);
		if($start_time){
			//创建连续活动读取上个活动的外观设置，直接跳转到第2步
			$EVENT = D("Events");
			$front_cfg = current($EVENT->where(Array("id"=>$eid,"sid"=>$this->sid))->getField("front_pic,curtool,notice,notice_rule,notice_receive,front_mob_cfg,front_pic,front_pic_url,front_cfg"));
			session("front_cfg",$front_cfg);
			$this->step2();
			die();
		}
		$Tool = D("CheckTools");
		$Tool->sid=$this->sid;
		$Tool->where="(`sm_tools`.`tool_type`=1 OR `sm_tools`.`tool_type`=3) AND `sm_tools_status`.`status`=1 ";
		$tools = $Tool->ShowTools();
		$this->assign('start_time',$start_time);
		$this->assign('tools',$tools);
		$this->display();
	}
	public function step2(){
		
		$create = I("create");
		$tool_code = I("tool");
		$start_time = I("start_time");
		$start_time = $start_time?date("Y-m-d H:i:s",strtotime($start_time)+1):"";
		
		if($create){
			session("lott_event_id",null);
			session("curtool",null);
			session("front_cfg",null);
			unset($_SESSION['lott_event_id']);
		}
		$this->assign('tool_code',$tool_code);
		$this->assign('name',$tool["name"]);
		$this->assign('pic_url',$tool["pic_url"]);
		$this->assign('tooltype',$tool["tool_type"]);
		$this->assign('start_time',$start_time);
		$this->assign('tool',$tool);
		$this->display("Event:step2");
	}
	public function step3(){
		$start_time = I("post.start_time");
		$end_time = I("post.end_time");
		
		if($start_time && $end_time){
			$EVENT = D("Events");
			$EVENT->c = $this->c;
			$EVENT->sid = $this->sid;
			$EVENT->start_time = $start_time;
			$EVENT->end_time = $end_time;
			$time_duble = $EVENT->check_time();
			// 检查时间
			if($time_duble && session("lott_event_id")!=$time_duble){
				$this->success("时间范围重叠，请重新选择时间范围。同时只能进行一个活动",U("Shopmanage/Event/Index"));
				die();
			}
			// 准备数据
			$data = $EVENT->prepare_data();
			// 插入或者更新
			// if(!session("curtool")){
				// $this->success("模版信息丢失，请重新创建");
			// }
			if(session("lott_event_id") && array_diff(session("lott_event_data"),$data)){
				$EVENT->where("`sid`=".$this->sid." AND `id`=".session("lott_event_id"))->save($data);
			}else{
				$prepare_front_cfg = session("front_cfg");
				if($prepare_front_cfg){
					//创建连续活动时，直接读取之前活动的前端设置
					$data["notice"] = $prepare_front_cfg["notice"];
					$data["notice_rule"] = $prepare_front_cfg["notice_rule"];
					$data["notice_receive"] = $prepare_front_cfg["notice_receive"];
					$data["front_mob_cfg"] = $prepare_front_cfg["front_mob_cfg"];
					$data["front_pic"] = $prepare_front_cfg["front_pic"];
					$data["front_pic_url"] = $prepare_front_cfg["front_pic_url"];
					$data["front_cfg"] = $prepare_front_cfg["front_cfg"];
				}
				$lott_event_id = $EVENT->add($data);
				session("lott_event_id",$lott_event_id);
			}
			session("lott_event_data",$data);
		}
		// 读取奖品
		$EGIFT = D("EventsGifts");
		$EGIFT->sid=$this->sid;
		$EGIFT->eid=session("lott_event_id");
		$EGIFT->getEventGifts();
		$gifts = $EGIFT->gifts;
		$gift_names = $EGIFT->gift_names;
		$gift_ids = $EGIFT->gift_ids;
		$left_prob = 10000000 - $EGIFT->sub_prob;
		$left_prob = $left_prob>0?$left_prob:0;
		
		$this->assign("range",mt_rand(100,999));
		$this->assign("left_prob",$left_prob);
		$this->assign("end_time",$end_time);
		$this->assign("gifts",$gifts);
		$this->display();
	}
	public function step4(){
		//选择要设置的外观类型，手机端和移动端
		$end_time = I("end_time");
		$curtool = session("curtool");
		//判断是否是支持移动外观的
		$has_mob = C("HAS_MOB");
		$this->assign("has_mob",in_array($curtool,$has_mob));
		$this->assign("end_time",$end_time);
		$this->assign("curtool",$curtool);
		$this->display();
	}
	public function step4_mob(){
		$eid = I("eid");
		$Tool = D("CheckTools");
		$Tool->sid=$this->sid;
		$Tool->where="(`sm_tools`.`tool_type`=4) AND `sm_tools_status`.`status`=1 ";
		$tools = $Tool->ShowTools();
		$this->assign('start_time',$start_time);
		$this->assign('tools',$tools);
		$this->assign('eid',$eid);
		$this->display("step4_show_tools");
	}
	public function step4_mob_cfg(){
		//设置移动端外观
		$end_time = I("end_time");
		$curtool = I("tool");
		$eid = I("eid");
		$eid = $eid?$eid:session("lott_event_id");
		
		$EVENT = D("Events");
		$EVENT->where(Array("sid"=>$this->sid,"id"=>$eid))->save(Array("mobtool"=>$curtool));
		$EVENT->sid=$this->sid;
		$EVENT->eid=$eid;
		$cfg = $EVENT->getMobCfg();
		
		if($curtool=="mob_dices"||$curtool=="mob_wheel"){
			$GFT=M("EventsGifts");
			$gft = $GFT->where(Array("event_id"=>$eid))->getField("id,name");
		}
		
		$this->assign("gft",$gft);
		$this->assign("cfg",$cfg);
		$this->assign("eid",$eid);
		$this->assign("end_time",$end_time);
		$this->assign("curtool",$curtool);
		$this->display("Event:step4_".$curtool);
	}
	public function step4_pc(){
		$eid = I("eid");
		$Tool = D("CheckTools");
		$Tool->sid=$this->sid;
		$Tool->where="(`sm_tools`.`tool_type`=1) AND `sm_tools_status`.`status`=1 ";
		$tools = $Tool->ShowTools();
		$this->assign('start_time',$start_time);
		$this->assign('tools',$tools);
		$this->assign('eid',$eid);
		$this->display("step4_show_tools");
	}
	public function step4_pc_cfg(){
		// 设置PC外观
		$end_time = I("end_time");
		$curtool = I("tool");
		$eid = I("eid");
		$eid = session("from_event_id")?session("from_event_id"):session("lott_event_id");
		$EVENT = D("Events");
		$EVENT->where(Array("sid"=>$this->sid,"id"=>$eid))->save(Array("curtool"=>$curtool));
		$EVENT->sid=$this->sid;
		$EVENT->eid=$eid;
		$cfg = $EVENT->getFrontCfg();
		$frount_default = $EVENT->frount_default;
		$gift_default = $EVENT->gift_default;

		$GFT = M("EventsGifts");
		$gifts = $GFT->where("`sid`=".$this->sid." AND `status`<>9 AND `event_id`=".session("lott_event_id"))->getField("id,type,level,name,status");
		$gifts["0"]["name"]="不中奖";
		$info["type"]="warning";
		$info["msg"]="亲，在进行操作时请先确认对店铺没有影响。";

		$this->assign("end_time",$end_time);
		$this->assign("eid",session("lott_event_id"));
		$this->assign("info",$info);
		$this->assign("issetted",$EVENT->issetted);
		$this->assign("sid",$this->sid);
		$this->assign("bg_default",C("DEFAULT_FRONT_PIC_URL")[$cfg["frontcfg"]["curtool"]]);
		$this->assign("frount_default",$frount_default);
		$this->assign("gift_default",$gift_default);
		$this->assign("cfg",$cfg);
		$this->assign("gifts",$gifts);
		$this->assign("title","绑定奖品");
		$this->assign("nextshow","step5");
		$this->display("Event:step4_pc_cfg");
	}
	public function step5_mob(){
		$end_time = I("end_time");
		$data["start_page"] = I("start_page");
		$data["dialog"] = I("dialog");
		$data["jinnang"] = I("jinnang");
		$data["cal"] = I("cal");
		$eid = I("eid");
		$mob = I("mob");
		if(!$eid){
			$this->success("没有关键数据！请返回",U("Shopmanage/event/index"));
			die();
		}
		$save["front_mob_cfg"]=json_encode($data);
		$save["notice"]=$data["start_page"]["notice_input"];
		$CFG = M("Events");
		$re = $CFG->where("`id`=".$eid." AND `sid`=".$this->sid)->save($save);
		if($re){
			if(I("nextshow")){
				$end_time = I("end_time");
				$end_time = $end_time?date("Y-m-d H:i:s",strtotime($end_time)+1):"";
				$this->assign("end_time",$end_time);
				$this->display();
			}else{
				$this->success("保存成功！",U("Shopmanage/edit/front_cfg",Array("eid"=>$eid,"mob"=>$mob,"skin"=>"mob")));
			}
		}else{
			$this->success("保存失败！请检查",U("Shopmanage/edit/front_cfg",Array("eid"=>$eid,"mob"=>$mob,"skin"=>"pc")));
		}
	}
	public function step5_pc(){

		$eid = I("eid");
		if(!$eid){
			$this->success("没有关键数据！请返回",U("Shopmanage/event/index"));
			die();
		}
		$gifts = I("gift");
		$width = I("width");
		$height = I("height");
		$left = I("left");
		$top = I("top");
		$forbidden = I("forbidden");
		$showpic = I("showpic");
		$kaiguan = I("kaiguan");
		$GFT = M("EventsGifts");
		$gift_pics = $GFT->where("`sid`=".$this->sid." AND `status`<>9")->getField("id,pic_url");//print_r($gift_pics);
		foreach($gifts as $k=>$gift){
			$tmp["giftset"][$k]["gift"]=$gifts[$k];
			$tmp["giftset"][$k]["width"]=$width[$k];
			$tmp["giftset"][$k]["height"]=$height[$k];
			$tmp["giftset"][$k]["left"]=$left[$k];
			$tmp["giftset"][$k]["top"]=$top[$k];
			$tmp["giftset"][$k]["forbidden"]=$forbidden[$k]==1?true:false;
			$tmp["giftset"][$k]["pic_url"]=$showpic[$k]==1?$gift_pics[$gifts[$k]]:"";
		}
		$tmp["onoff"]=$kaiguan;
		$data = $tmp;
		//整理奖品绑定
		
		//整理外观配置	
		$data["contdown"]["left"] = I("leftcontdown")."px";
		$data["contdown"]["top"] = I("topcontdown")."px";
		$data["contdown"]["visibility"] = I("fbcontdown")?"hidden":"visible";;
		
		$data["gift"]["left"] = I("leftgift")."px";
		$data["gift"]["top"] = I("topgift")."px";
		$data["gift"]["visibility"] = I("fbgift")?"hidden":"visible";;
		
		$data["mywin"]["left"] = I("leftmywin")."px";
		$data["mywin"]["top"] = I("topmywin")."px";
		$data["mywin"]["visibility"] = I("fbmywin")?"hidden":"visible";;
		
		$data["gg"]["left"] = I("leftgg")."px";
		$data["gg"]["top"] = I("topgg")."px";
		$data["gg"]["visibility"] = I("fbgg")?"hidden":"visible";;
		
		$data["win"]["left"] = I("leftwin")."px";
		$data["win"]["top"] = I("topwin")."px";
		$data["win"]["visibility"] = I("fbwin")?"hidden":"visible";;
		
		$data["chance"]["left"] = I("leftchance")."px";
		$data["chance"]["top"] = I("topchance")."px";
		$data["chance"]["visibility"] = I("fbchance")?"hidden":"visible";;
		
		$data["action"]["left"] = I("leftaction")."px";
		$data["action"]["top"] = I("topaction")."px";
		$data["action"]["width"] = I("widthaction")."px";
		$data["action"]["height"] = I("heightaction")."px";
		$data["action"]["background"] = "url(".I("action-pic").") no-repeat";
		
		$data["cart"]["left"] = I("leftcart")."px";
		$data["cart"]["top"] = I("topcart")."px";
		$data["cart"]["width"] = I("widthcart")."px";
		$data["cart"]["height"] = I("heightcart")."px";
		$data["cart"]["background"] = "url(".I("cart-pic").") no-repeat";
		
		$data["jinnang"]["left"] = I("leftjinnang")."px";
		$data["jinnang"]["top"] = I("topjinnang")."px";
		$data["jinnang"]["width"] = I("widthjinnang")."px";
		$data["jinnang"]["height"] = I("heightjinnang")."px";
		$data["jinnang"]["visibility"] = I("fbjinnang")?"hidden":"visible";;
		// $data["jinnang"]["background"] = "url(".I("jinnang-pic").") no-repeat";
		$data["jinnang"]["pic"] = I("jinnang-pic");
		
		$data["sign_main"]["left"] = I("leftsign_main")."px";
		$data["sign_main"]["top"] = I("topsign_main")."px";
		$data["sign_main"]["width"] = I("widthsign_main")."px";
		$data["sign_main"]["height"] = I("heightsign_main")."px";
		$data["sign_main"]["background"] = "url(".I("sign_main-pic").") no-repeat";
		
		$data["qdbtn"]["visibility"] = I("fbqdbtn")?"hidden":"visible";;
		$data["qdbtn"]["left"] = I("leftqdbtn")."px";
		$data["qdbtn"]["top"] = I("topqdbtn")."px";
		$data["qdbtn"]["width"] = I("widthqdbtn")."px";
		$data["qdbtn"]["height"] = I("heightqdbtn")."px";
		$data["qdbtn"]["set"] = I("qdbtn_set");
		$data["qdbtn"]["size"] = I("qdbtn_size");
		$data["qdbtn"]["style"] = I("qdbtn_style");
		$data["qdbtn"]["picurl"] = I("qdbtn_picurl");
		$data["qdbtn"]["text"] = "签到";
		$data["qdbtn"]["tp"] = "btn";		
		
		$data["jfdhbtn"]["visibility"] = I("fbjfdhbtn")?"hidden":"visible";;
		$data["jfdhbtn"]["left"] = I("leftjfdhbtn")."px";
		$data["jfdhbtn"]["top"] = I("topjfdhbtn")."px";
		$data["jfdhbtn"]["width"] = I("widthjfdhbtn")."px";
		$data["jfdhbtn"]["height"] = I("heightjfdhbtn")."px";
		$data["jfdhbtn"]["set"] = I("jfdhbtn_set");
		$data["jfdhbtn"]["size"] = I("jfdhbtn_size");
		$data["jfdhbtn"]["style"] = I("jfdhbtn_style");
		$data["jfdhbtn"]["picurl"] = I("jfdhbtn_picurl");
		$data["jfdhbtn"]["text"] = "积分兑换";
		$data["jfdhbtn"]["tp"] = "btn";
		
		$data["qddhbtn"]["visibility"] = I("fbqddhbtn")?"hidden":"visible";;
		$data["qddhbtn"]["left"] = I("leftqddhbtn")."px";
		$data["qddhbtn"]["top"] = I("topqddhbtn")."px";
		$data["qddhbtn"]["width"] = I("widthqddhbtn")."px";
		$data["qddhbtn"]["height"] = I("heightqddhbtn")."px";
		$data["qddhbtn"]["set"] = I("qddhbtn_set");
		$data["qddhbtn"]["size"] = I("qddhbtn_size");
		$data["qddhbtn"]["style"] = I("qddhbtn_style");
		$data["qddhbtn"]["picurl"] = I("qddhbtn_picurl");
		$data["qddhbtn"]["text"] = "签到兑换";
		$data["qddhbtn"]["tp"] = "btn";
		
		$data["qdjcbtn"]["visibility"] = I("fbqdjcbtn")?"hidden":"visible";;
		$data["qdjcbtn"]["left"] = I("leftqdjcbtn")."px";
		$data["qdjcbtn"]["top"] = I("topqdjcbtn")."px";
		$data["qdjcbtn"]["width"] = I("widthqdjcbtn")."px";
		$data["qdjcbtn"]["height"] = I("heightqdjcbtn")."px";
		$data["qdjcbtn"]["set"] = I("qdjcbtn_set");
		$data["qdjcbtn"]["size"] = I("qdjcbtn_size");
		$data["qdjcbtn"]["style"] = I("qdjcbtn_style");
		$data["qdjcbtn"]["picurl"] = I("qdjcbtn_picurl");
		$data["qdjcbtn"]["text"] = "签到加成";
		$data["qdjcbtn"]["tp"] = "btn";
		
		$data["ddhjfbtn"]["visibility"] = I("fbddhjfbtn")?"hidden":"visible";;
		$data["ddhjfbtn"]["left"] = I("leftddhjfbtn")."px";
		$data["ddhjfbtn"]["top"] = I("topddhjfbtn")."px";
		$data["ddhjfbtn"]["width"] = I("widthddhjfbtn")."px";
		$data["ddhjfbtn"]["height"] = I("heightddhjfbtn")."px";
		$data["ddhjfbtn"]["set"] = I("ddhjfbtn_set");
		$data["ddhjfbtn"]["size"] = I("ddhjfbtn_size");
		$data["ddhjfbtn"]["style"] = I("ddhjfbtn_style");
		$data["ddhjfbtn"]["picurl"] = I("ddhjfbtn_picurl");
		$data["ddhjfbtn"]["text"] = "订单号积分";
		$data["ddhjfbtn"]["tp"] = "btn";
		
		$data["jifen_rule"]["visibility"] = I("fbjifen_rule")?"hidden":"visible";;
		$data["jifen_rule"]["left"] = I("leftjifen_rule")."px";
		$data["jifen_rule"]["top"] = I("topjifen_rule")."px";
		$data["jifen_rule"]["width"] = I("widthjifen_rule")."px";
		$data["jifen_rule"]["height"] = I("heightjifen_rule")."px";
		
		
		$uploadbg = I("uploadbg");
		if($uploadbg){
			$save["front_pic_url"]=$uploadbg;
			$save["front_pic"]=1;
		}
		$save["front_cfg"]=json_encode($data);
		
		$CFG = M("Events");
		$re = $CFG->where("`id`=".$eid." AND `sid`=".$this->sid)->save($save);
		if($re){
			if(I("nextshow")){
				$end_time = I("end_time");
				$end_time = $end_time?date("Y-m-d H:i:s",strtotime($end_time)+1):"";
				$this->assign("end_time",$end_time);
				$this->display();
			}else{
				$this->success("保存成功！",U("Shopmanage/edit/front_cfg?eid=".$eid));
			}
		}else{
			$this->success("保存失败！请检查",U("Shopmanage/edit/front_cfg?eid=".$eid));
		}
	}
}